package com.zx.controller;

import com.zx.bean.Message;
import com.zx.bean.Treat;
import com.zx.mvc.ResponseBody;
import com.zx.service.TreatService;
import com.zx.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class TreatController {
    @ResponseBody("/admin/treat.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        Treat treat=new Treat();
        treat.setName(request.getParameter("name"));
        treat.setA(request.getParameter("a"));
        treat.setB(request.getParameter("b"));//健康码颜色
        treat.setC(request.getParameter("c"));//病人体温
        treat.setD(request.getParameter("d"));
        treat.setE(request.getParameter("e"));
        treat.setF(request.getParameter("f"));
        treat.setG(request.getParameter("g"));
        treat.setH(request.getParameter("h"));
        treat.setI(request.getParameter("i"));
        treat.setJ(request.getParameter("j"));
        treat.setK(request.getParameter("k"));
        treat.setL(request.getParameter("l"));
        treat.setM(request.getParameter("m"));

        String timelong="";
        String symptom="";
        String suggest="";
        boolean flag=false;
        timelong=treat.getA();
        String healthCodeColur=treat.getB();

        double temperature=Double.parseDouble(treat.getC());

        if(!treat.getD().equals("无此症状")){
           symptom+=treat.getD()+" ";
        }

        if (!treat.getE().equals("无此症状")){
            symptom+=treat.getE()+" ";
        }
        if (!treat.getF().equals("无此症状")){
            symptom+=treat.getF()+" ";
        }
        if(!treat.getG().equals("无此症状")){
            symptom+=treat.getG()+" ";
        }
        if(!treat.getH().equals("无此症状")){
            symptom+=treat.getH()+" ";
        }
        if(!treat.getI().equals("无此症状")){
            symptom+=treat.getI()+" ";
        }
        if(!treat.getJ().equals("无此症状")){
            symptom+=treat.getJ()+" ";
        }
        if(!treat.getK().equals("无此症状")){
            symptom+=treat.getK()+" ";
        }
        if(!treat.getL().equals("无此症状")){
            symptom+=treat.getL()+" ";
        }
        if(!treat.getM().equals("无此症状")){
            symptom+=treat.getM()+" ";
        }
        String fever="";

        if(healthCodeColur.equals("红")||healthCodeColur.equals("黄")){
            flag=true;
            fever="------警报-----健康码不正常，颜色为: "+healthCodeColur+" 感染新冠风险较大，请尽快就医且隔离!";
        }else if(temperature<37.3){
            flag=false;
            fever="温度正常，健康码颜色正常,正常感冒";
        }else if(temperature>=37.3&&temperature<=38.5){
            flag=true;
            fever="-----警报-----您正中低发热中！温度为:"+temperature+"可能感染新冠，请尽快就医隔离！";
        }else if(temperature>38.5&&temperature<=39.1){
            flag=true;
            fever="-----警报-----您正高度发热中！温度为:"+temperature+"感染新冠风险较大，请尽快就医隔离！";
        }else if(temperature>39.1){
            flag=true;
            fever="检测对象可能不是活着的人类！";
        }
        if(flag==false){
            if(!treat.getK().equals("无此症状")||!treat.getL().equals("无此症状")||!treat.getM().equals("无此症状")){

                suggest="您的感冒是重症，请及时线上就医,患病时长为"+timelong+"症状为"+symptom+"已经存入汇仁薄，祝您早日康复！";
                System.out.println("k:"+treat.getK()+" l:"+treat.getL()+" m:"+treat.getM().length());
            }else {

                suggest="您的感冒是轻症状,患病时长为"+timelong+"症状为"+symptom+"推荐使用感冒类药品，如：感康。症状已经存入汇仁薄，祝您早日康复！";
            }
            Date date=new Date();
            String treatTime =date.toString();
            boolean result= TreatService.insert(timelong, symptom,suggest,treat.getName(),treatTime);
            Message msg=null;
            if(result){
                msg=new Message(0,suggest);
            }else{
                msg=new Message(-1,suggest);
            }
            String json= JsonUtil.toJson(msg);
            return json;

        }else{
            suggest=fever;
            Message msg=new Message(1,suggest);
            String json=JsonUtil.toJson(msg);
            return  json;
        }

    }
    @ResponseBody("/treat/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        String name=request.getParameter("name");
        Treat entity=TreatService.findByName(name);
        Message msg=new Message();
        if(entity==null){
            msg.setStatus(-1);
            msg.setResult("姓名不存在");
        }else{
            msg.setData(entity);
            msg.setStatus(0);
        }
        String s=JsonUtil.toJson(msg);
        return s;
    }
}
